package com.syx.extensiondemo.Listener;

import com.rabbitmq.client.Channel;
import com.syx.extensiondemo.Constant.Constants;
import org.springframework.amqp.core.Message;
import org.springframework.amqp.rabbit.annotation.RabbitListener;
import org.springframework.stereotype.Component;

import java.io.IOException;
import java.nio.charset.StandardCharsets;

@Component
public class DlListener {
    @RabbitListener(queues = Constants.NORMAL_QUEUE)
    public void handMassage(Message message, Channel channel) throws IOException {
        long deliveryTag = message.getMessageProperties().getDeliveryTag();
        try {
            System.out.println(message.getMessageProperties().getDeliveryTag()+"++++++++++++++++++"+new String(message.getBody(), StandardCharsets.UTF_8));
            System.out.println("业务处理中");
            int num=3/0;
            System.out.println("业务处理完成");
            channel.basicAck(deliveryTag,false);


        }catch (Exception e){
            //变成死信队列
            channel.basicNack(deliveryTag,false,false);

        }

    }

    @RabbitListener(queues = Constants.DL_QUEUE)
    public void DlHandMassage(Message message, Channel channel) throws IOException {
        long deliveryTag = message.getMessageProperties().getDeliveryTag();
        try {

            System.out.println("死信队列"+message.getMessageProperties().getDeliveryTag()+"++++++++++++++++++"+new String(message.getBody(), StandardCharsets.UTF_8));
                /*System.out.println("业务处理中");
                *//*int num=3/0;*//*
                System.out.println("业务处理完成");*/
            channel.basicAck(deliveryTag,false);


        }catch (Exception e){
            //变成死信队列
            channel.basicNack(deliveryTag,false,false);



        }

    }
}
